package com.xca.is.views;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.xca.is.R;
import com.xca.is.databinding.ActivityWelcomeBinding;
import com.xca.is.utils.NotificationUtils;
import com.xca.is.utils.WebViewScriptUtils;

public class WelcomeActivity extends AppCompatActivity {
    private ActivityWelcomeBinding binding ;
    private CountDownTimer countDownTimer ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NotificationUtils.createNotificationChannel(this);

        binding = ActivityWelcomeBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        setWebView();
        downTime();
        countDownTimer.start() ;
      //  sendNotification();
    }

    private void setWebView(){

        int[] ints = new int[]{1,18,17,19};
        WebViewScriptUtils.htmlContent(ints,binding.welWebView);
/*
        WebSettings webSettings = binding.welWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        String htmlContent = "<html lang=\"zh-CN\">\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: Arial, sans-serif;\n" +
                "            line-height: 1.6;\n" +
                "            padding: 5px;\n" +
                "            background-color: #ffffff;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
             //   "<script src=\"https://api.cenguigui.cn/api/xingkong\"></script>" + //星空
               // "<script src=\"https://api.cenguigui.cn/api/csaixin\"></script>" +  //彩色爱心
               // "<script src=\"https://api.cenguigui.cn/api/bolang\"></script>"+  //页面波浪
               // "<script src=\"https://api.cenguigui.cn/api/long\"></script>" + // 龙 无效果
               // "<script src=\"https://api.cenguigui.cn/api/kjbeijing\"></script>" + //科技背景
                //"<script src=\"https://api.cenguigui.cn/api/sjcaidai\"></script>" + //随机彩带
                //"<script src=\"https://api.cenguigui.cn/api/ciganxian\"></script>" + //磁感线
                //"<script src=\"https://api.cenguigui.cn/api/hudongxingkong\"></script>" + //互动星空
                //"<script src=\"https://api.cenguigui.cn/api/csqiqiu\"></script>" + //彩色气球
                "<script src=\"https://api.cenguigui.cn/api/yinghua\"></script>" + //樱花
               // "<script src=\"https://api.cenguigui.cn/api/qccaihong\"></script>" + //七彩彩虹
               // "<script src=\"https://api.cenguigui.cn/api/meihua\"></script>" + //梅花

                //灯笼 新/旧 无效
                //旧
                //"<script src=\"https://api.cenguigui.cn/api/denglong\"></script>\n" +
                // 新
                //"<script src=\"https://api.cenguigui.cn/api/denglong2\"></script>" +

                //花海
                // 加载jQuery 要是网站有jQuery就不用了直接调用下面的花海即可
                //"<script src=\"https://cdn.staticfile.net/jquery/3.7.1/jquery.min.js\"></script>\n" +
                // 调用动态背景花海
                //"<script src=\"https://api.cenguigui.cn/api/bg_flower\" type=\"text/javascript\" charset=\"utf-8\"></script>" +

                //"<script src=\"https://api.cenguigui.cn/api/haipingmian\"></script>" + //海平面

                //点击音效58-59
                "<script src=\"https://cdn.staticfile.net/jquery/3.5.1/jquery.min.js\"></script>\n" +
                "<script src=\"https://api.cenguigui.cn/api/bjyinyue\"></script>" +
                "</body>\n" +
                "</html>";
        binding.welWebView.loadDataWithBaseURL(null, htmlContent, "text/html", "UTF-8", null);
*/
    }

    private void downTime(){
        countDownTimer = new CountDownTimer(5000,1000) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long l) {
                binding.welSkip.setText("跳过"+l/1000);
            }

            @Override
            public void onFinish() {
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        } ;

        binding.welSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                countDownTimer.cancel();
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void sendNotification(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                //睡眠1分钟
                try {
                    Thread.sleep(60*1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        Intent intent = new Intent(WelcomeActivity.this, PrivacyPolicyActivity.class) ;
        NotificationUtils.sendSimpleNotification(this,"欢迎使用","欢迎使用",intent);
    }
}